package 成绩排序;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
//                int num1  = s1.getChinesescore() + s1.getMathscore();
//                int num2 =s2.getChinesescore()+s2.getChinesescore();
//                int num = num2 - num1;
                int num = s2.getSum() - s1.getSum();
                //主要条件

                int num2 = num == 0 ? s1.getChinesescore() - s2.getChinesescore() : num;//防止同成绩总数无法存储
                int num3 =num2==0 ? s1.getName().compareTo(s2.getName()):num2;//防止同成绩无法存储
                //次要条件
                return num3;
            }
        });

        Student s1 = new Student("姜恒星", 55, 66);
        Student s2 = new Student("姜恒", 66, 45);
        Student s3 = new Student("姜", 40, 55);
        Student s4 = new Student("姜", 55, 40);
        Student s5=new Student("姜",55,40);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);


        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getChinesescore() + "," + s.getMathscore() + ","+s.getSum());
        }
    }
}
